<?php
/**
 * This file contains the class PersonNoticesDB which provides
 * all of the database requirements for the person notices page
 *
 * @copyright  2009 A Memory Tree
 * @license    A Memory Tree License
 */
/**
 * This class provides access to the database for PersonNoticesDB
 *
 * @copyright  2009 A Memory Tree
 * @license    A Memory Tree License
 */
class PersonNoticesDB
{
    /**
      * Provides access to the database
     *
     * @var Db
     */
    private $_db;
    /**
     * Create a new instance of this class
     * 
     * @return void
     * @param Db $db
     */
    function __construct($db)
    {
        $this->_db = $db;
    }
    /**
     * Escape the provided value
     *
     * @return string
     * @param mixed $value
     */
    function escape($value)
    {
        $db = $this->_db;
        return $db->escape($value);
    }
    /**
     * Retrieve a persons details
     *
     * @return array[string]mixed
     * @param int $personId
     */
    function getPerson($personId)
    {
        $db = $this->_db;
        $person = Array();
        $sql = <<< EOT
            SELECT
                Person.ID, 
                Person.Surname, 
                Person.FirstNames, 
                Person.FormerName, 
                Person.NeeName, 
                Person.AlsoKnownAs, 
                Person.DeathDate, 
                Person.BirthDate, 
                Person.Age AS Age, 
                Person.AgeMeasure AS AgeMeasure, 
                Country.Name AS Country
            FROM
                Person 
                    LEFT JOIN Country
                        ON Person.CountryID = Country.ID
            WHERE
                Person.ID = %d
EOT;
        $sql = sprintf($sql, $personId);
        $result = $db->query($sql);
        if ($db->rowCount($result) > 0) {
            $row = $db->fetch($result);
            $person['id'] = $row['ID'];
            $person['surname'] = $row['Surname'];
            $person['firstNames'] = $row['FirstNames'];
            $person['formerName'] = $row['FormerName'];
            $person['neeName'] = $row['NeeName'];
            $person['alsoKnownAs'] = $row['AlsoKnownAs'];
            $person['deathDate'] = $row['DeathDate'];
            $person['age'] = $row['Age'];
            $person['ageMeasure'] = $row['AgeMeasure'];
            $person['country'] = $row['Country'];
        }
        return $person;
    }
    /**
     * Retrieve the publications associated with a person
     *
     * @return array[string]mixed
     * @param int $personId
     */
    function getPublications($personId)
    {
        $db = $this->_db;
        $publications = array();
        $sql = <<< EOT
            SELECT
                Name, DatePublished
            FROM
                Publication
                    LEFT JOIN PublicationPerson
                        ON Publication.ID = PublicationPerson.PublicationID
            WHERE
                PublicationPerson.PersonID = %d
EOT;
        $sql = sprintf($sql, $personId);
        $result = $db->query($sql);
        while (1) {
            $row = $db->fetch($result);
            if (empty($row)) {
                break;
            }
            $publication = array(
                'name' => $db->unescape($row['Name']),
                'date' => $db->unescape($row['DatePublished'])
            );
            array_push($publications, $publication);
        }
        return $publications;
    }
    /**
     * Retrieve the newspaper name
     *
     * @return string
     * @param string $publicationName
     */
    function getNewspaperName($paperCode)
    {
        $db = $this->_db;
        $name = '';
        $sql = <<< EOT
            SELECT Name
            FROM Newspapers
            where ShortName = '%s'
EOT;
        $sql = sprintf($sql, $paperCode);
        $result = $db->query($sql);
        if ($db->rowCount($result)) {
            $row = $db->fetch($result);
            $name = $row['Name'];
        }
        return $name;
    }
}
?>